package todomanager.CONTROLLER;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;

import todomanager.MODEL.TodoManagerModel;
import todomanager.View.MainFrame;
import todomanager.View.IconManager;

/**
 * A controller for the add frame
 * 
 * @author jonasbirgersson
 * 
 */
public class AddFrameController {
	private TodoManagerModel todoModel;
	private MainFrame mainFrameView;

	private Action addTaskAction;
	private Action addMoreAction;
	private Action cancelAction;

	/**
	 * Standard constructor for the add frame
	 * 
	 * @param model
	 *            the TodoManagerModel for database interaction
	 * @param view
	 *            the main frame for updating the table and so on.
	 * @throws UnsupportedEncodingException 
	 */
	public AddFrameController(TodoManagerModel model, MainFrame view) throws UnsupportedEncodingException {
		this.todoModel = model;
		this.mainFrameView = view;

		IconManager icm = new IconManager();

		this.addTaskAction = new AddTaskAction(view.getLabel("addTaskButton"),
				null);
		this.addMoreAction = new AddMoreAction(
				view.getLabel("addMoreTasksButton"),
				icm.createIcon("New16.gif"));
		this.cancelAction = new CancelAction(view.getLabel("cancelButton"),
				null);

		// Add Actions
		this.mainFrameView.getAddView().addButtonActions(this.addTaskAction,
				this.cancelAction, this.addMoreAction);
	}

	/**
	 * Action for adding a task to the database
	 * 
	 * @author jonasbirgersson
	 * 
	 */
	public class AddTaskAction extends AbstractAction {

		public AddTaskAction(String text, ImageIcon icon) throws UnsupportedEncodingException {
			super(text, icon);
			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("addTaskButton_Desc"));
		}

		public void actionPerformed(ActionEvent e) {
			if (mainFrameView.getAddView().getUserInput() != null) {
				todoModel.addTask(mainFrameView.getAddView().getUserInput());
				mainFrameView.updateTableData();
				mainFrameView.getAddView().setVisible(false);
			}
		}
	}

	/**
	 * Action for adding more tasks without the need of closing the window and
	 * open it again.
	 * 
	 * @author jonasbirgersson
	 * 
	 */
	public class AddMoreAction extends AbstractAction {

		public AddMoreAction(String text, ImageIcon icon) throws UnsupportedEncodingException {
			super(text, icon);
			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("addMoreTasksButton_Desc"));
		}

		public void actionPerformed(ActionEvent e) {
			// System.exit(0);
		}
	}

	/**
	 * Action to cancel adding a task.
	 * 
	 * @author jonasbirgersson
	 * 
	 */
	public class CancelAction extends AbstractAction {

		public CancelAction(String text, ImageIcon icon) throws UnsupportedEncodingException {
			super(text, icon);
			putValue(SHORT_DESCRIPTION,
					mainFrameView.getLabel("cancelButton_Desc"));
		}

		public void actionPerformed(ActionEvent e) {
			mainFrameView.getAddView().setVisible(false);
		}
	}
}
